﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmnhomchuhh : DevExpress.XtraEditors.XtraForm
    {
         thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        // select danh sach nhom chu hh
        private DataTable f_selectdsnhomchuhh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom chu hh co dieu kien
        private DataTable f_selectnhomchuhhdk(string tendonvitinh)
        {

            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 1, 2, tendonvitinh };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // insert them mot nhom chu hang hoa
        private int f_insertnhomchuhh(string tennhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 2, 1, tennhomchuhh };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }

        // update mot don vi tinh
        private int f_updatenhomchuhh(string manhomchuhh, string tennhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME" };
            o = new object[] { 3, 1, manhomchuhh, tennhomchuhh };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }
        // xoa nhom chu hang hoa
        private int f_deletenhomchuhh(string manhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, manhomchuhh };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }
        public mh_dmnhomchuhh()
        {
            InitializeComponent();
        }

        private void mh_dmnhomchuhh_Load(object sender, EventArgs e)
        {
              luoi_nhomchuhh.MainView = grid_dmnhomchuhh;
            luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();

            bton_luu.Enabled = false;
            bton_tennhomchuhh.Enabled = false;
        }

        private void grid_dmnhomchuhh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_dmnhomchuhh.FocusedColumn;
            txt_tennhomchuhh.Text = grid_dmnhomchuhh.GetRowCellDisplayText(chiso, col_tennhomchuhh) + "";

            bton_luu.Enabled = false;
            bton_tennhomchuhh.Enabled = false;
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            string tennhomchuhh = "";
            if (txt_timtennhomchuhh.Text != "")
                tennhomchuhh = "'%" + txt_timtennhomchuhh.Text + "%'";
            else
                tennhomchuhh = txt_timtennhomchuhh.Text;
            luoi_nhomchuhh.DataSource = null;
            luoi_nhomchuhh.DataSource = f_selectnhomchuhhdk(tennhomchuhh);
        }

        private void bton_tennhomchuhh_Click(object sender, EventArgs e)
        {
            mh_chu frm_chu = new mh_chu();
            frm_chu.ControlBox = false;

            Variable.str_position_frmchu = "bton_tendonvitinh";
            frm_chu.ShowDialog();
            txt_tennhomchuhh.Text = Variable.str_chu;
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tennhomchuhh.Text = "";
            bton_luu.Enabled = true;
            bton_tennhomchuhh.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_tennhomchuhh.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_nhomchuhh.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manhomchuhh = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletenhomchuhh(manhomchuhh);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_nhomchuhh.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_nhomchuhh.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;

            if (txt_tennhomchuhh.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertnhomchuhh(txt_tennhomchuhh.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string manhomchuhh = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh).ToString();
                ketqua = f_updatenhomchuhh(manhomchuhh, txt_tennhomchuhh.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
               // luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            }
            luoi_nhomchuhh.DataSource = null;
            luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();
            }
        }
       
    }
}